package ivan.bookKeeper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class TestActivity extends Activity {
	private static final String TAG = TestActivity.class.getSimpleName();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>(2);
		params.add(new BasicNameValuePair("content1", "aa"));
		params.add(new BasicNameValuePair("prefix1", "AU"));

		HttpPost post = new HttpPost("http://bisis.bgb.rs/SingleQuery");
		try {
			post.setEntity(new UrlEncodedFormEntity(params));
		} catch (UnsupportedEncodingException e) {
			Log.d(TAG, "ERROR WITH POST ENTITY! : " + e.getMessage());
		}
		
		HttpClient client = new DefaultHttpClient();
		try {
			HttpResponse response = client.execute(post);
			InputStream responseBody = response.getEntity().getContent();
			Log.d(TAG, "SUCCESS!!");
			StringBuilder builder = new StringBuilder();
			BufferedReader br = new BufferedReader(new InputStreamReader(responseBody));
			String line;
			while ((line=br.readLine()) != null)
			{
				builder.append(line);
			}  
			Log.d(TAG, builder.toString());
			
		} catch (ClientProtocolException e) {
			Log.d(TAG, "CLIENT PROTOCOL EXCEPTION: " + e.getMessage());
		} catch (IOException e) {
			Log.d(TAG, "IOEXCEPTION : " + e.getMessage());
		}	
	}
}
